/*Jeff Finn
 * CSC 342
 * Adv Database Spring 2014
 * Team 4
 */

package view;

import model.Customer;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.FactoryOrder;

public class FactoryOrderDAO {
	
	private DBConnect con = null;
	private long lnoexist;
	
	public FactoryOrderDAO (DBConnect incon)
	{
		con = incon;
		try
		{
			con.getConnection().setAutoCommit(false);
		}
		catch (SQLException e)
		{
			controller.ErrorHandle.LogError(e, "FactoryOrderDAO");
		}
	}
	
	public FactoryOrder CreateFactoryOrder(FactoryOrder inFactoryOrder)
	{
		PreparedStatement ps= null;
		
		try
		{
			ps = con.getConnection().prepareStatement("insert into "+con.getSchema()+".Factory_Order (Orderid, Customerid, Order_date" +
        		"values(?,?,?)");
			if(inFactoryOrder.getOrderId() != lnoexist )
			{
				 ps.setLong(1, inFactoryOrder.getOrderId());
				 ps.setLong(2, inFactoryOrder.getCustomer().getCustomerId());
				 ps.setDate(3, (Date) inFactoryOrder.getOrderDate());
				 ps.executeUpdate();
			}
			else
			{
				System.out.println("\nCould not Create Factory Order");
				System.out.println("\n" + ps.toString());
			}
			
		}
		catch (SQLException e)
		{
			controller.ErrorHandle.LogError(e, "CreateFactoryOrder");
		}
		return inFactoryOrder;
	}
	
	//Returns one FactoryOrder - requires Primary Key - Orderid+Customerid
	public FactoryOrder ViewFactoryOrder(FactoryOrder inFactoryOrder)
	{
			if(inFactoryOrder.getOrderId() != lnoexist &&  inFactoryOrder.getCustomer().getCustomerId() != lnoexist)
			{
				 return ViewFactoryOrderSet(inFactoryOrder)[0];
			}
			else
			{
				System.out.println("\nCould not Create Factory Order - need Orderid&Customerid");
			}
		return inFactoryOrder;

	}
	
	public FactoryOrder[] ViewFactoryOrderSet(FactoryOrder inFactoryOrder)
	{
		FactoryOrder[] outFactoryOrder;
		PreparedStatement ps= null;
		ResultSet rs = null;
		
		
		try
		{
			String Query = "Select (Orderid, Customerid, Order_date" +
	        		""+con.getSchema()+".Factory_Order";
			
			if(inFactoryOrder.getOrderId() != lnoexist
			|| inFactoryOrder.getCustomer().getCustomerId() != lnoexist
			|| inFactoryOrder.getOrderDate() != null)
			{
				Query += " Where";
				if(inFactoryOrder.getOrderId() != lnoexist)
					Query += " Orderid = " + inFactoryOrder.getOrderId() + " And";
				if(inFactoryOrder.getOrderId() != lnoexist)
					Query += " Customerid = " + inFactoryOrder.getCustomer().getCustomerId() + " And";
				if(inFactoryOrder.getOrderId() != lnoexist)
					Query += " Orderid = " + inFactoryOrder.getOrderId() + " And";
				Query = Query.substring(0, Query.length()-3);
			}
			
			
			ps = con.getConnection().prepareStatement(Query);
			rs = ps.executeQuery();
			
			outFactoryOrder = new FactoryOrder[rs.getFetchSize()];
			
			while(rs.next())
			{
				outFactoryOrder[rs.getRow()] = BuildNewFactoryOrder(rs.getLong(1), rs.getLong(2), rs.getDate(3));
			}
			
			return outFactoryOrder;
		}
		catch (SQLException e)
		{
			controller.ErrorHandle.LogError(e, "CreateFactoryOrder");
		}
		return null;

	}
	
	public int UpdateFactoryOrder(FactoryOrder inFactoryOrder)
	{
		PreparedStatement ps= null;
		
		try
		{
			ps = con.getConnection().prepareStatement(
					"Update "+con.getSchema()+".Factory_Order "
					+ "Set Order_date = ? "
					+ "where Orderid=? and Customerid=?");
			if(inFactoryOrder.getOrderId() != lnoexist )
			{
				 ps.setLong(2, inFactoryOrder.getOrderId());
				 ps.setLong(3, inFactoryOrder.getCustomer().getCustomerId());
				 ps.setDate(1, (Date) inFactoryOrder.getOrderDate());
				 return ps.executeUpdate();
			}
			else
			{
				System.out.println("\nCould not Update Factory Order");
				System.out.println("\n" + ps.toString());
			}
			
		}
		catch (SQLException e)
		{
			controller.ErrorHandle.LogError(e, "CreateFactoryOrder");
		}
		return 0;
	}
	
	public int DeleteFactoryOrder(FactoryOrder inFactoryOrder)
	{
		PreparedStatement ps= null;
		
		try
		{
			ps = con.getConnection().prepareStatement(
					"Delete From "+con.getSchema()+".Factory_Order "
					+ "where Orderid=? and Customerid=?");
			if(inFactoryOrder.getOrderId() != lnoexist )
			{
				 ps.setLong(1, inFactoryOrder.getOrderId());
				 ps.setLong(2, inFactoryOrder.getCustomer().getCustomerId());
				 return ps.executeUpdate();
			}
			else
			{
				System.out.println("\nCould not Update Factory Order");
				System.out.println("\n" + ps.toString());
			}
			
		}
		catch (SQLException e)
		{
			controller.ErrorHandle.LogError(e, "CreateFactoryOrder");
		}
		return 0;
	}
	
	public FactoryOrder BuildNewFactoryOrder(long orderId, long customerId, Date orderDate)
	{
		FactoryOrder justBuilt = new FactoryOrder();
		justBuilt.setOrderId(orderId);
		justBuilt.setOrderDate(orderDate);
		
		Customer temp = new Customer();
		temp.setCustomerId(customerId);
		justBuilt.setCustomer(temp);
		
		
		return justBuilt;
	}
}
